package entity;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

import main.ManagementFrame;

import entity.Database.DataType;
import entity.Database.*;

import storage.DataAccessManager;

public class Tab 
{
	private ManagementFrame parentFrame;
	private JScrollPane pane;
	private JTable jtable;
	private Database database;
	private Table table;
	private DefaultTableModel model;
	private boolean isSaved;
	public Tab(ManagementFrame parentFrame,JScrollPane pane,JTable jtable,Table table,Database database)
	{
		this.parentFrame = parentFrame;
		this.pane = pane;
		this.jtable = jtable;
		this.table = table;
		this.database = database;
		this.isSaved = true;
		jtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		model = new DefaultTableModel();
		pane.setViewportView(jtable);
		jtable.setModel(model);
	}
	
	public JScrollPane getPane()
	{
		return this.pane;
	}
	
	public void setPane(JScrollPane pane)
	{
		this.pane = pane;
	}
	
	public JTable getJTable()
	{
		return this.jtable;
	}
	
	public void setJTable(JTable jtable)
	{
		this.jtable = jtable;
	}
	
	public Table getTable()
	{
		return this.table;
	}
	
	public void setTable(Table table)
	{
		this.table = table;
	}
	
	public Database getDatabase()
	{
		return this.database;
	}
	
	public DefaultTableModel getModel()
	{
		return this.model;
	}
	
	public boolean IsSaved()
	{
		return this.isSaved;
	}
	
	public void setIsSaved(boolean isSaved)
	{	
		this.isSaved = isSaved;
	}
	public void refreshTable()
	{
		model = new DefaultTableModel();
		for (int i=0;i<table.getColumnsCount();i++) model.addColumn(table.getColumnAt(i).getName()+" (" + table.getColumnAt(i).getType().toString()+")");
		for (int i=0;i<table.getRowCount();i++)
		{
			List<String> rowData = new ArrayList<String>();
			for (int j=0;j<table.getColumnsCount();j++)
			{
				rowData.add(table.getColumnAt(j).getDataAt(i));
			}
			model.addRow(rowData.toArray());
		}
		jtable.setModel(model);
		model.addTableModelListener(new TableModelListener() 
		{
			public void tableChanged(TableModelEvent e) 
			{
				isSaved=false;
				parentFrame.tabbedPane.setTitleAt(parentFrame.tabbedPane.getSelectedIndex(), "*Edit "+parentFrame.tabs.get(parentFrame.tabbedPane.getSelectedIndex()).getTable().getName());
				if (e.getType()==TableModelEvent.UPDATE)
				{
					Column column = table.getColumnAt(e.getColumn());
					Object value = model.getValueAt(e.getFirstRow(), e.getColumn());
					if (value==null) return;
					if (column.getType()==DataType.Int)
					{
						String s = (String) model.getValueAt(e.getFirstRow(), e.getColumn());
						if (s.compareTo("")==0) 
						{
							model.setValueAt(null, e.getFirstRow(), e.getColumn());
							return;
						}
						
						try
						{
							Integer.parseInt(s);
						}
						catch (NumberFormatException NFE)
						{
							JOptionPane.showMessageDialog(null, "Invalid input. Please input the right data type.");
							model.setValueAt(null, e.getFirstRow(), e.getColumn());
							return;
						}
					}
					String s = (String) value;
					if (column.IsPrimaryKey()) for (int i=0;i<model.getRowCount();i++) 
						if (model.getValueAt(i, e.getColumn())!=null)
						{
							if (((String)model.getValueAt(i, e.getColumn())).compareTo(s)==0 && i!=e.getFirstRow()) 
							{
								JOptionPane.showMessageDialog(null, "Invalid input. This column is primary key and can not have duplicate row.");
								model.setValueAt(null, e.getFirstRow(), e.getColumn());
								return;
							}
						}
				}
		    }
		});
	}
	
	public int Save()
	{
		TableCellEditor editor = jtable.getCellEditor();
		if (editor!=null) editor.stopCellEditing();
		table.clearColumns();
		table.setRowCount(model.getRowCount());
		for (int i=0;i<model.getRowCount();i++)
		{
			for (int j=0;j<table.getColumnsCount();j++)
			{	
				String data;
				if (model.getValueAt(i, j)==null || ((String) model.getValueAt(i, j)).compareTo("")==0)
				{
					if (table.getPrimaryKey()==j)
					{
						JOptionPane.showMessageDialog(null, "Save error. One or more primary key is blank.");
						return -1;
					}
					data="";
				}
				else data = (String) model.getValueAt(i, j);
				table.getColumnAt(j).addData(data);
			}
		}
		isSaved = true;
		return 0;
	}
}
